package com.gdou.crrp.service;

import com.gdou.crrp.entity.*;
import com.gdou.crrp.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ReservationService {

    @Autowired
    private ReservationRepository reservationRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DoctorRepository doctorRepository;

    @Autowired
    private BillRepository billRepository;

    //获取所以医院
    public List<Hospital> findAllHospital(){
        return hospitalRepository.findAll();
    }

    //根据医院名称获取对应的科室
    public List<Department> findDepartmentByHospitalName(String name){
        Hospital hospital = hospitalRepository.findByName(name);
        return departmentRepository.findByHid(hospital.getId());
    }

    //根据医院名称和科室名称查找对应医生
    public List<Doctor> findDoctorByHnameAndDname(String hname, String dname){
        Hospital hospital = hospitalRepository.findByName(hname);
        Department department = departmentRepository.findByNameAndHid(dname, hospital.getId());
        return doctorRepository.findByDid(department.getDid());
    }

    //根据医院名称和科室名称和医生姓名找医生
    public Doctor findDoctorByHnameAndDnameAndName(String hname, String dname, String name){
        Hospital hospital = hospitalRepository.findByName(hname);
        Department department = departmentRepository.findByNameAndHid(dname, hospital.getId());
        return doctorRepository.findByDidAndName(department.getDid(), name);
    }

    //获取医生可预约时间
    public List<Date> getDoctorWorkTime(Doctor doctor){
        List<Date> list = new ArrayList<>();
        List<Integer> integerList = new ArrayList<>();
        String work = doctor.getWork();
        for (int i = 0; i < 14; i++) {
            if(work.charAt(i)=='1') {
                switch(i){
                    case 0:
                        integerList.add(10);
                        break;
                    case 1:
                        integerList.add(11);
                        break;
                    case 2:
                        integerList.add(20);
                        break;
                    case 3:
                        integerList.add(21);
                        break;
                    case 4:
                        integerList.add(30);
                        break;
                    case 5:
                        integerList.add(31);
                        break;
                    case 6:
                        integerList.add(40);
                        break;
                    case 7:
                        integerList.add(41);
                        break;
                    case 8:
                        integerList.add(50);
                        break;
                    case 9:
                        integerList.add(51);
                        break;
                    case 10:
                        integerList.add(60);
                        break;
                    case 11:
                        integerList.add(61);
                        break;
                    case 12:
                        integerList.add(0);
                        break;
                    case 13:
                        integerList.add(1);
                }
            }
        }
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        int year, month, day;
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(year, month, day, 0, 0, 0);
        for(int i=0; i<14; i++){
            int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
            int ap = calendar.get(Calendar.AM_PM);
            int sum = week*10 + ap;
            if(integerList.contains(sum)) {
                list.add(calendar.getTime());
            }
            calendar.add(Calendar.HOUR_OF_DAY, 12);
        }
        System.out.println(list);
        return list;
    }

    /**
     * 添加预约
     * @param phone：用户手机号，即登录账号
     * @param hname：医院名称
     * @param dname：科室名称
     * @param name：医生姓名
     * @param time：预约时间
     * @return 添加成功的预约单
     */
    public Reservation save(String phone, String hname, String dname, String name, Date time){
        List<Reservation> list = reservationRepository.findByHospitalNameAndDepartmentNameAndDoctorNameAndTime(hname, dname, name, time);
        if(list.size()<10){
            User user = userRepository.findByPhone(phone);
            Hospital hospital = hospitalRepository.findByName(hname);
            Department department = departmentRepository.findByNameAndHid(dname, hospital.getId());
            Doctor doctor = doctorRepository.findByDidAndName(department.getDid(), name);
            Reservation reservation = new Reservation();
            reservation.setHospitalName(hname);
            reservation.setDepartmentName(dname);
            reservation.setDoctorName(name);
            reservation.setTime(time);
            reservation.setPay(5);
            reservation.setStatus("待付款");
            reservation.setUserName(user.getName());
            reservation.setUserPhone(phone);
            reservation.setIsEvaluate(0);
            reservation.setDoctorPhone(doctor.getPhone());
            reservation.setOrderTime(new Date());
            Reservation save = reservationRepository.save(reservation);
            Bill bill = new Bill();
            bill.setUid(user.getId());
            bill.setBillId(save.getId());
            billRepository.save(bill);
            return save;
        }
        else
            return null;
    }

    //进行预约付款
    public String pay(Integer id, Integer pay){
        Reservation reservation = reservationRepository.getOne(id);
        User user = userRepository.findByPhone(reservation.getUserPhone());
        if(user.getMoney()>pay){
            user.setMoney(user.getMoney() - pay);
            User save1 = userRepository.save(user);
            reservation.setStatus("已付款");
            Reservation save = reservationRepository.save(reservation);
            if(save!=null&& save1!=null)
                return "支付完成，预约成功！";
            else
                return "预约失败！";
        }
        return "账户余额不足，请充值！";
    }

    //根据用户id查找所有订单
    public List<Bill> findBillByUid(Integer uid){
        List<Bill> billList = billRepository.findAllByUid(uid);
        billList.sort(Comparator.comparing(Bill::getBillId).reversed());
        return billList;
    }

    //根据预约单id查找预约单
    public Reservation findReservationById(Integer id){
        return reservationRepository.getOne(id);
    }

    //进行充值
    public String recharge(int id ,int money){
        User one = userRepository.getOne(id);
        one.setMoney(one.getMoney() + money);
        User save = userRepository.save(one);
        if(save!=null)
            return "充值成功！";
        else
            return "充值失败，请重新充值！";
    }

    //根据用户id获取用户
    public User getUserById(int id){
        return userRepository.getOne(id);
    }

    //根据模糊医院名称获取医院
    public List<Hospital> findHospital(String name){
        String s = "%" + name  + "%";
        return hospitalRepository.findByNameLike(s);
    }

    //根据医院名称和模糊科室名称获取科室
    public List<Department> findDepartment(String hname, String name){
        Hospital hospital = hospitalRepository.findByName(hname);
        String s = "%" + name  + "%";
        return departmentRepository.findByHidAndNameLike(hospital.getId(),s);
    }

    //根据预约单id取消预约
    public void cancelReserve(int id){
        reservationRepository.deleteById(id);
        billRepository.deleteById(id);
    }

}
